package im.yisme.view.ui.util;

import im.yisme.R;
import im.yisme.view.activities.MusicLibraryActivity;
import java.util.ArrayList;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MusicLibAdapter extends BaseAdapter{
    private ArrayList<Map<String, Object>> data;
	private MusicLibraryActivity activity;
	private Context context;
    
    public MusicLibAdapter(MusicLibraryActivity activity , Context context , ArrayList<Map<String, Object>> data){
    	
    	this.activity = activity;
    	this.data = data;
        this.context = context;
    }
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		MusicLibViewHolder holder = null;
		
		if(convertView == null){
			
			convertView = LayoutInflater.from(context).inflate(R.layout.musiclib_listitem, null);
			
			holder = new MusicLibViewHolder();
			holder.musicName = (TextView) convertView.findViewById(R.id.textView_name);
			holder.delete = (Button) convertView.findViewById(R.id.button_delete);	
			convertView.setTag(holder);
		}
		else {
			holder = (MusicLibViewHolder) convertView.getTag();
		}
		
		Map<String, Object> map = data.get(position);
		
		holder.musicName.setText((String)map.get("name"));
		
		holder.delete.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				activity.deleteMusic(position);
			}
		});
		
		
		
		return convertView;
	}

}
